package com.yyryyty.chapter5;

import java.util.Arrays;
import java.util.List;

/**
 * @Description :
 * @Author : YTY
 * @Date : 2021-01-17 15:00
 */
public class Mapping {

    public static void main(String[] args) {

//        List<String> dishNames = menu.stream()
//                .map(Dish::getName)
//                .collect(toList());
//        System.out.println(dishNames);

//        List<String> words = Arrays.asList("Java 8", "Lambdas", "In", "Action!");
//        words.stream()
//                .map(String::length)
//                .forEach(System.out::println);

        // 菜名长度大于6的个数
//        System.out.println(menu.stream()
//                .map(Dish::getName)
//                .mapToInt(String::length)
//                .filter(nameLength -> nameLength > 6)
//                .max().getAsInt());

        // flatMap
//        List<String> words = Arrays.asList("Hello", "World");
//
//        List<String[]> result = words.stream()
//                .map(word -> word.split(""))
//                .distinct()
//                .collect(toList());

//        List<Stream<String>> result = words.stream()
//                .map(word -> word.split(""))
//                .map(Arrays::stream)
//                .distinct()
//                .collect(toList());

//        List<String> result = words.stream()
//                .map(w -> w.split(""))
//                .flatMap(Arrays::stream)
//                .distinct()
//                .collect(toList());

        // 5.2 作业
//        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
//        numbers.stream()
//                .map(i -> i * i)
//                .forEach(System.out::println);

        // 查找和匹配
//        if (menu.stream().anyMatch(Dish::isVegetarian)) {
//            System.out.println("The menu is vegetarian friendly");
//        }

//        if (menu.stream().allMatch(dish -> dish.getCalories() < 1000)) {
//            System.out.println("The menu is very healthy!");
//        }

//        if (menu.stream().noneMatch(dish -> dish.getCalories() >= 1000)) {
//            System.out.println("The menu is very healthy!");
//        }

//        menu.stream()
//                .filter(Dish::isVegetarian)
//                .findAny()
//                .ifPresent(dish -> System.out.println(dish.getName()));

        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
        numbers.stream()
                .map(x -> x * x)
                .filter(x -> x % 3 == 0)
                .findFirst()
                .ifPresent(System.out::println);
    }
}
